import random

import pygame

from pygame.sprite import Sprite

from exp2.bullet import Bullet
from exp2.ship import Ship


class Alien(Ship):
    """A class to represent a single alien in the fleet."""

    def __init__(self, ai_game):
        """Initialize the alien and set its starting position."""
        super().__init__(ai_game)
        self.ai_game = ai_game
        self.screen = ai_game.screen
        self.settings = ai_game.settings

        # Load the alien image and set its rect attribute.
        # 使用Settings类中的image_base_path属性
        self.image = pygame.image.load(self.settings.image_base_path + "alien.png")
        self.rect = self.image.get_rect()

        # Start each new alien near the top left of the screen.
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        # Store the alien's exact horizontal position.
        self.x = float(self.rect.x)

        self.bullets = pygame.sprite.Group()  # 存储子弹的编组

    def fire_bullet(self):
        """
        发射子弹
        """
        if len(self.bullets) < self.settings.bullets_allowed:
            new_bullet = Bullet(self.screen, self.settings, self.rect.midbottom, -1)
            self.ai_game.aliens_bullets.add(new_bullet)

    def check_edges(self):
        """Return True if alien is at edge of screen."""
        screen_rect = self.screen.get_rect()
        return (self.rect.right >= screen_rect.right) or (self.rect.left <= 0)

    def update(self):
        """Move the alien right or left."""
        pass


